package com.zw.weiyi.util;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;

/**
 * 对通话记录操作
 * @author Striver_zw
 *   Date  2014-12-19
 */
public class CallUtil {
	private static Cursor cursor;
	/**
	 * 负责存放呼叫记录的内容提供者源码在 ContactsProvider 项目下：
	 * 源码路径：com/android/providers/contacts/CallLogProvider.java
	 * 使用到的数据库在：/data/data/com.android.providers.contacts/databases/contacts2.db
	 *   表名:calls
	 *   呼叫记录有三种类型：
	 *   来电：CallLog.Calls.INCOMING_TYPE （常量值：1）
	 *   已拨：CallLog.Calls.OUTGOING_TYPE（常量值：2）
	 *   未接：CallLog.Calls.MISSED_TYPE（常量值：3）
	 *   
	 *   需要声明的权限
	 *   <uses-permission android:name="android.permission.READ_CONTACTS" />
	 *   <uses-permission android:name="android.permission.WRITE_CONTACTS" />
	 *   
	 *   4. 系统的通话记录，是通过 ContentProvider 来对外共享的
	 *   5. Uri
	 *   CallLog.Calls.CONTENT_URI : 等价于：Uri.parse("content://call_log/calls");
	 *   6. 查询出所有记录
	 *   ContentResolver resolver = getContentResolver();
	 *   resolver.query(CallLog.Calls.CONTENT_URI, null, null, new String[]{"15101689022"}, null);
	 *   7. 查询某一个联系人的所有记录（按电话号码）
	 *   resolver.query(CallLog.Calls.CONTENT_URI, null, "number=?", new String[]{"15101689022"}, null);
	 *   8. 查询某一个联系人的所有未接电话记录（按电话号码）
	 *   resolver.query(CallLog.Calls.CONTENT_URI, null, "number=? and type=3", new String[]{"15101689022"}, null);
	 *   8. 删除某联系人最近的一次来电
	 */
	/* 根据类型来删除 */
	public static void deleteCall(Context context,String Type,String phone){
		ContentResolver resolver = context.getContentResolver();
		
		Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, new String[]{"_id"}, "number=? and (type="+Type+")",  new String[]{phone},  "_id desc limit 1");
		if(cursor.moveToFirst()) {
			int id = cursor.getInt(0);
			resolver.delete(CallLog.Calls.CONTENT_URI, "_id=?", new String[] {id + ""});
		}
//		System.out.println("delete call ok");
	}
	//所有通话次数
	public static int seleteCall(Context context,String phone){
		ContentResolver resolver = context.getContentResolver();
		  cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, "number=?", new String[]{phone}, null);
		  int count = cursor.getCount();
		  return count;			
	}
	
	//未接电话记录
	public static int seleteCallget3(Context context,String phone){
		ContentResolver resolver = context.getContentResolver();
		cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, "number=? and type=3", new String[]{phone}, null);
		int count = cursor.getCount();
		return count;			
	}
	//来接 1
	public static int seleteCallget1(Context context,String phone){
		ContentResolver resolver = context.getContentResolver();
		cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, "number=? and type=1", new String[]{phone}, null);
		int count = cursor.getCount();
		return count;			
	}
	//播出 2
	public static int seleteCallget2(Context context,String phone){
		ContentResolver resolver = context.getContentResolver();
		cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, "number=? and type=2", new String[]{phone}, null);
		int count = cursor.getCount();
		return count;			
	}
}
